package com.fc.mall.portal.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.model.UmsIntegrationChangeHistory;
import com.fc.mall.model.UmsMember;
import com.fc.mall.portal.domain.IntegerationParam;
import com.fc.mall.portal.dto.MemberIntegration;
import com.fc.mall.portal.service.MemberIntegrationService;
import com.fc.mall.portal.service.UmsMemberService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Sunny on 2019/6/20.
 */
@RestController
@Api(tags = "UmsMemberIntegrationController", description = "会员积分")
@RequestMapping("/member/integration")
public class UmsMemberIntegrationController {

    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private MemberIntegrationService memberIntegrationService;

    @GetMapping
    public Result handleGetMemberIntegration(IntegerationParam param)
    {
        UmsMember member = memberService.getCurrentMember();
        List<UmsIntegrationChangeHistory> integrationChangeHistoryLis = memberIntegrationService.getMemberIntegrationByMemberId(member.getId(), param);
        Integer totalIntegration = 0;
        if(!integrationChangeHistoryLis.isEmpty())
        {
            for(UmsIntegrationChangeHistory history : integrationChangeHistoryLis)
            {
                totalIntegration += history.getChangeCount();
            }
        }
        MemberIntegration memberIntegration = new MemberIntegration();
        memberIntegration.setIntegration(totalIntegration);
        memberIntegration.setHistoryIntegration(member.getHistoryIntegration());
        memberIntegration.setIntegrationChangeHistoryList(integrationChangeHistoryLis);

        return Result.success(memberIntegration);
    }
}
